
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.XJ;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.XJ
{
	/// <summary>
	/// XJMaintenanceComment service
	/// </summary>
	public partial class XJMaintenanceCommentService : IXJMaintenanceCommentService
	{
		#region Constants

		/// <summary>
		/// Key for caching
		/// </summary>
		private const string XJMAINTENANCECOMMENTS_ALL_KEY = "Nop.xjmaintenancecomment.all";
		/// <summary>
		/// Key for caching
		/// </summary>
		/// <remarks>
		/// {0} : xJMaintenanceComment ID
		/// </remarks>
		private const string XJMAINTENANCECOMMENT_BY_ID_KEY = "Nop.xjmaintenancecomment.id-{0}";
		/// <summary>
		/// Key pattern to clear cache
		/// </summary>
		private const string XJMAINTENANCECOMMENTS_PATTERN_KEY = "Nop.xjmaintenancecomment.";
		#endregion

		#region Fields
		private readonly IRepository<XJMaintenanceComment> _xJMaintenanceCommentRepository;
		private readonly IEventPublisher _eventPublisher;
		private readonly ICacheManager _cacheManager;
		#endregion

		#region Ctor

		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="cacheManager">Cache manager</param>
		/// <param name="xJMaintenanceCommentRepository">XJMaintenanceComment repository</param>
		/// <param name="eventPublisher">Event published</param>
		public XJMaintenanceCommentService(ICacheManager cacheManager,
			IRepository<XJMaintenanceComment> xJMaintenanceCommentRepository,
			IEventPublisher eventPublisher)
		{
			this._cacheManager = cacheManager;
			this._xJMaintenanceCommentRepository = xJMaintenanceCommentRepository;
			this._eventPublisher = eventPublisher;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Gets all XJMaintenanceComments
		/// </summary>
		/// <returns>XJMaintenanceComments</returns>
		public virtual IList<XJMaintenanceComment> GetAllXJMaintenanceComments()
		{
			string key = XJMAINTENANCECOMMENTS_ALL_KEY;
			return _cacheManager.Get(key, () =>
			{
				var query = from a in _xJMaintenanceCommentRepository.Table
							orderby a.Id
							select a;
				return query.ToList();
			});
		}

		public virtual IPagedList<XJMaintenanceComment> GetAllMaintenanceComments(int maintenanceId = 0, int pId = 0, string name = "", int pageIndex = 0, int pageSize = int.MaxValue)
		{
			var query = _xJMaintenanceCommentRepository.Table;
			if (maintenanceId > 0)
				query = query.Where(m => m.MaintenanceId == maintenanceId);
			if (pId > 0)
				query = query.Where(m => m.PId == pId);
			query = query.Where(c => !c.Deleted);
			query = query.OrderByDescending(c => c.Id);

			//paging
			return new PagedList<XJMaintenanceComment>(query, pageIndex, pageSize);
		}

		/// <summary>
		/// Gets a XJMaintenanceComment 
		/// </summary>
		/// <param name="xJMaintenanceCommentId">XJMaintenanceComment identifier</param>
		/// <returns>XJMaintenanceComment</returns>
		public virtual XJMaintenanceComment GetXJMaintenanceCommentById(int xJMaintenanceCommentId)
		{
			if (xJMaintenanceCommentId == 0)
				return null;

			string key = string.Format(XJMAINTENANCECOMMENT_BY_ID_KEY, xJMaintenanceCommentId);
			return _cacheManager.Get(key, () => _xJMaintenanceCommentRepository.GetById(xJMaintenanceCommentId));
		}

		/// <summary>
		/// Inserts a XJMaintenanceComment
		/// </summary>
		/// <param name="xJMaintenanceComment">XJMaintenanceComment</param>
		public virtual void InsertXJMaintenanceComment(XJMaintenanceComment xJMaintenanceComment)
		{
			if (xJMaintenanceComment == null)
				throw new ArgumentNullException("xJMaintenanceComment");

			_xJMaintenanceCommentRepository.Insert(xJMaintenanceComment);

			_cacheManager.RemoveByPattern(XJMAINTENANCECOMMENTS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityInserted(xJMaintenanceComment);
		}

		/// <summary>
		/// Updates the XJMaintenanceComment
		/// </summary>
		/// <param name="xJMaintenanceComment">XJMaintenanceComment</param>
		public virtual void UpdateXJMaintenanceComment(XJMaintenanceComment xJMaintenanceComment)
		{
			if (xJMaintenanceComment == null)
				throw new ArgumentNullException("xJMaintenanceComment");

			_xJMaintenanceCommentRepository.Update(xJMaintenanceComment);

			_cacheManager.RemoveByPattern(XJMAINTENANCECOMMENTS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityUpdated(xJMaintenanceComment);
		}

		/// <summary>
		/// Deletes a XJMaintenanceComment
		/// </summary>
		/// <param name="xJMaintenanceComment">XJMaintenanceComment</param>
		public virtual void DeleteXJMaintenanceComment(XJMaintenanceComment xJMaintenanceComment)
		{
			if (xJMaintenanceComment == null)
				throw new ArgumentNullException("xJMaintenanceComment");

			_xJMaintenanceCommentRepository.Delete(xJMaintenanceComment);

			_cacheManager.RemoveByPattern(XJMAINTENANCECOMMENTS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityDeleted(xJMaintenanceComment);
		}

		#endregion
	}
}
